﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Pages_Dangky : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    private Entities en = new Entities();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        txtDiaChi.Text = "";
        txtHoTen.Text = "";
        txtMaKH.Text = "";
        txtPass.Text = "";
        txtPhone.Text = "";
        txtRePass.Text = "";
        txtUser.Text = "";
        txtUser.Focus();
    }
    protected void btnDangKy_Click(object sender, EventArgs e)
    {
      
        string makh = this.txtMaKH.Text;
        string tenkh = this.txtHoTen.Text;
        string diachiKH = this.txtDiaChi.Text;
        string phone = this.txtPhone.Text;
        string u = this.txtUser.Text;
        string p = "";
        if (txtPass.Text.Equals(txtRePass.Text.ToString()))
        {
            p = this.txtPass.Text;
        }
        else {
            Label1.Text = "Nhập lại mật khẩu chưa đúng!";
            txtRePass.Focus();
        }
        if (ExistsKH(makh) != true && ExistsTenTruyCap(u) !=true)
        {
            SqlCommand comm = new SqlCommand("Insert_KH", da.SqlConn());
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@MaKH", makh);
            comm.Parameters.AddWithValue("@TenKH", tenkh);
            comm.Parameters.AddWithValue("@DiaChi", diachiKH);
            comm.Parameters.AddWithValue("@Phone", phone);
            comm.Parameters.AddWithValue("@user", u);
            comm.Parameters.AddWithValue("@pass", en.EncodePassword(p.Trim(), ""));
            try
            {
                comm.Connection.Open();
                if (comm.ExecuteNonQuery() == -1)
                {
                    Response.Redirect("DangNhap.aspx");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                comm.Connection.Close();
                comm.Connection.Dispose();
                Response.Redirect("DangNhap.aspx");
            }
        }
        else
        {
            HttpContext.Current.Response.Write("<script language=\"javascript\">alert('Mã khách hàng này đã tồn tại hoặc tên truy cập đã tồn tại !');</script>");
        }
    }
        protected bool ExistsKH(string strMaKH)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("isExistsKH", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@maKH", strMaKH);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }
    protected bool ExistsTenTruyCap(string tentc)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("ExistsTenTruyCap", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@tentruycap", tentc);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }
}
